/**  
 * @Title: LocalLog.java
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-04-18 14:18:56 
 */  
package com.fsxgt.datagrid.core.task;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.ConfigurablePropertyResolver;
import org.springframework.core.env.PropertyResolver;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fsxgt.datagrid.core.utils.LogHelpper;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**  
 * @ClassName: LocalLog
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-04-18 14:18:56 
*/
@Slf4j
@Component
public class StatusHelper extends PropertySourcesPlaceholderConfigurer{
	
	private static PropertyResolver propertyResolver;

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
                                     ConfigurablePropertyResolver propertyResolver) throws BeansException {
        super.processProperties(beanFactoryToProcess, propertyResolver);
        this.propertyResolver = propertyResolver;
    }

	
	@Data
	public static class Log{
		private transient SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		private String seq;

		private transient Date datetime = new Date();
		private String msg;
		private String status;

		public Log(String seq,String msg,String status) {
			this.msg=msg;
			this.seq=seq;
			this.status=status;
		}

		public String getMsg() {
			return msg;
		}
	}
	
	public static String BEGIN="begin";
	public static String RUNNING="running";
	public static String END="end";
	public static String FLOWSTART="flowstart";
	public static String FLOWOVER="flowover";
	public static String ERROR="error";
	
	
	public static String INTERRUPTED="interrupted";
	
	public static String  INTERRUPTEDERROR="interruptederror";
	
	public static String FLOWERROR="flowerror";


	
	@Value("${locallog.path:\"\"}")
	private String path;
	@Value("${executor.address:\"\"}")
	private String server;
	
	

	
	public static void record(String execId,String msg,String status)  {
		
		Log bo = new Log(null,msg,status);
		record(msg,status,null,bo);
	}
	public static void record(String msg,String status,String seq ,Log bo)  {
		
		log.info(JSON.toJSONString(new Log(seq,msg,status)));

		
//		String p=getPath(execId);
//		FileUtil.appendUtf8String("\n"+com.alibaba.fastjson.JSON.toJSONString(new FlowExecLogBO(seq,msg,status)),p);
	}
	
	
	public static void record(SubTask subTask ,String msg) {
		record(subTask,msg,RUNNING);
	}
	
	public static void record(SubTask subTask ,String msg,String status)  {
		String s=status+"["+subTask.getName()+"]";
		if(msg!=null&&msg.length()>0) {
			s=s+"-"+msg;
		}
		record(s,status,subTask.getId(),new Log(subTask.getId(),msg,status));
	}
	
	public static String getPath(String execId) {
		String p=propertyResolver.getProperty("locallog.path")+"/"+execId+".log";
		return p;
	}
	
	public static List getLog(String execId,Date logDate) throws IOException {
		return LogHelpper.readLines(logDate, execId);
//		String p=getPath(execId);
//		return FileUtil.readUtf8Lines(new File(p));
	}
	
	public static Map getLogView(String execId, Date logDate) throws IOException {
		// TODO Auto-generated method stub

		Map result = new HashMap();

		List<String> list = StatusHelper.getLog(execId, logDate);

		StringBuilder msg = new StringBuilder();

		Map nodes = new HashMap();

		for (String s : list) {

			String json = null;
			if (s.indexOf("- {\"msg\":\"") != -1) {
				json = s.substring(s.indexOf("- {") + 1);
			}

			if (json != null) {
				StatusHelper.Log log = JSONObject.parseObject(json, StatusHelper.Log.class);
				if (log.getStatus().equals(StatusHelper.FLOWOVER)) {
					result.put("status", StatusHelper.FLOWOVER);
				}
				if (log.getSeq() != null) {
					nodes.put(log.getSeq(), log.getStatus());
				}
			}
			msg.append(s).append("\n");
		}

		result.put("nodes", nodes);
		result.put("msg", msg.toString());

		return result;
	}
}
